package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AssetHandleFacilityBean
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.StorehouseBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class AssetHandleFacilityDamageViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var parentCompanyId: Int = 0
    var currentPage = 1
    var pageSize = 10
    var storehouseId: Int? = 0
    val uc = UiChangeEvent()
    var beanList: MutableList<AssetHandleFacilityBean> = arrayListOf()

    var sStorehouseList: MutableList<StorehouseBean.StoreHouseListBean> = arrayListOf()
    var storehouseNameList: MutableList<String> = arrayListOf()
    var storehouseInfo = ObservableField("请选择仓库")
    var facilityNoObs = ObservableField("")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<ListBean<AssetHandleFacilityBean>> = SingleLiveEvent()
        val loadSubmitEvent: SingleLiveEvent<MutableList<AssetHandleFacilityBean>> = SingleLiveEvent()
        val loadChangeEvent: SingleLiveEvent<Int> = SingleLiveEvent()
    }

    val reSearch: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(0)
    })
    val selectedStore: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.loadChangeEvent.postValue(1)
    })
    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        this.getProcessDamageProList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 1
        this.getProcessDamageProList()
    })

    val selectedSubmit: BindingCommand<Void> = BindingCommand(BindingAction {
        var selectedList: MutableList<AssetHandleFacilityBean>? = arrayListOf()
        for (bean in beanList!!) {
            if (bean.selected!=null && bean.selected!!) {
                selectedList?.add(bean)
            }
        }
        uc.loadSubmitEvent.postValue(selectedList)
    })

    fun getStorehouseList (
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId
    ) {
        var keeperInfo: Int = 0
        model.mTengYunHttpDataSource.getOrgUseManageStorehouse(
            GsonUtils.toJson(
                mapOf(
                    "companyId" to companyId,
                    "userId" to userId,
                    "keeperInfo" to keeperInfo
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<StorehouseBean>>() {
                override fun onResult(t: BaseBean<StorehouseBean>) {
                    if (t.state == 0) {
                        sStorehouseList = t.data?.list as MutableList<StorehouseBean.StoreHouseListBean>
                        for(bean in sStorehouseList){
                            storehouseNameList.add(bean.storehouseName!!)
                        }
                    }
                    dismissLoading()
                }
                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
    fun getDataList () {
        if (storehouseId==null||storehouseId!! < 1)return
        getProcessDamageProList()
    }

    private fun getProcessDamageProList() {
        val applyId: Int? = null
        val isAssets: Boolean? = true
        val productCode: String? = facilityNoObs.get()
        val productName: String? = ""
        val status: Int? = 1
        model.mTengYunHttpDataSource.getProcessDamageProListStore(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "applyId" to applyId,
                    "isAssets" to isAssets,
                    "productCode" to productCode,
                    "productName" to productName,
                    "propCompanyId" to parentCompanyId,
                    "status" to status,
                    "storehouseId" to storehouseId
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleSortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleSortBean>>) {
                    if (t.state == 0) {
                        selectFacilityListInStroerHouseByCompanyIdAndUserId(t.data?.list)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }


    private fun selectFacilityListInStroerHouseByCompanyIdAndUserId(
        sortList: List<AssetHandleSortBean>?,
        companyId: String? = model.getLoginCompanyId(),
        userId: String? = model.getLocalUserInfo()?.userId,
    ) {
        if (sortList == null || sortList.isEmpty()){
            val listBean = ListBean<AssetHandleFacilityBean>()
            listBean.hasNextPage = false
            listBean.total = 0
            listBean.list = arrayListOf()
            uc.loadCompleteEvent.postValue(listBean)
            return
        }
        var facilityCodeList :MutableList<String> =  arrayListOf()
        for (bean in sortList){
            facilityCodeList.add(bean.productCode!!)
        }
        val facilityName: String? = ""
        val facilityNo: String? = facilityNoObs.get()
        val handleApplicationId: String? = null
        val type: Int? = 1
        model.mTengYunHttpDataSource.selectFacilityListInStroerHouseByCompanyIdAndUserId(
            GsonUtils.toJson(
                mapOf(
                    "pageNo" to currentPage,
                    "pageSize" to pageSize,
                    "userId" to userId,
                    "companyId" to companyId,
                    "facilityName" to facilityName,
                    "facilityNo" to facilityNo,
                    "handleApplicationId" to handleApplicationId,
                    "parentCompanyId" to parentCompanyId,
                    "type" to type,
                    "storehouseId" to storehouseId,
                    "facilityCodeList" to facilityCodeList
                )
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<AssetHandleFacilityBean>>>() {
                override fun onResult(t: BaseBean<ListBean<AssetHandleFacilityBean>>) {
                    if (t.state == 0) {
                        if (currentPage == 1) {
                            beanList.clear()
                        }
                        beanList.addAll(t.data?.list!!)
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}